package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckitemDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckitemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckitemService.class)
@Transactional
public class CheckitemServiceImpl implements CheckitemService {
    @Autowired
    private CheckitemDao checkitemDao;


    @Override
    public void addCheckitem(CheckItem checkItem) {
        checkitemDao.addCheckitem(checkItem);
    }

    /**
     * 带条件分页查询
     *
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return
     */
    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        Page<CheckItem> page = checkitemDao.findPage(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 删除检查项
     *
     * @param id
     */
    @Override
    public void deleteCheckitem(Integer id) {
        //业务逻辑
        //查询检查项是否包含在检查组里
        int count = checkitemDao.findCheckGroupCountByCheckItemId(id);
        if (count > 0) {
            throw new RuntimeException("检查项存在于检查组中！");
        }
        checkitemDao.deleteCheckitem(id);

    }

    /**
     * 查询检查项
     * @param id
     * @return
     */
    @Override
    public CheckItem findCheckitemByid(Integer id) {
        return checkitemDao.findCheckitemByid(id);
    }

    /**
     * 编辑检查项
     * @param checkItem
     */
    @Override
    public void updateCheckitem(CheckItem checkItem) {
        checkitemDao.updateCheckitem(checkItem);
    }

    /**
     * 查询所有检查项
     * @return
     */
    @Override
    public List<CheckItem> findAll() {
        return checkitemDao.findAll();
    }
}
